package cn.wolfcode.shop.controller;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.config.annotation.Reference;

import cn.wolfcode.shop.domain.Brand;
import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.domain.Product;
import cn.wolfcode.shop.domain.ProductSku;
import cn.wolfcode.shop.domain.SkuProperty;
import cn.wolfcode.shop.qo.PageResult;
import cn.wolfcode.shop.qo.ProductQueryObject;
import cn.wolfcode.shop.service.IBrandService;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.service.IPropertyValueService;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.shop.util.UploadUtil;
import cn.wolfcode.shop.vo.JSONResult;
import cn.wolfcode.shop.vo.ProductVo;

/**
 * Created by luohaipeng on 2018/1/10.
 */
@Controller
@RequestMapping(value = "/products")
public class ProductController {

	//	@Value("${file.path}")
	@Value("${spring.http.multipart.location}")
	private String filePath;

	@Reference
	IProductService productService;
	@Reference
	ICatalogService catalogService;
	@Reference
	IBrandService brandService;
	@Reference
	IPropertyService propertyService;
	@Reference
	IPropertyValueService propertyValueService;

	@Reference
	ISkuPropertyService skuPropertyService;

	/**
	 * 商品列表界面
	 */
	@GetMapping(value = "/view")
	public String product() {
//		System.out.println("客户端mgrsite");
		return "product/product";
	}

	/**
	 * 具体的商品数据
	 */
	@GetMapping(value = "")
	public String productPage(ProductQueryObject qo, Map map) {
		PageResult pageResult = productService.productPage(qo);
		// map也可以将数据共享到作用域,Model底层也是存成map,要SpringMvc帮我们创建的,自己创建的没用
		map.put("pageResult", pageResult);
		return "product/product_list";
	}

	@RequestMapping(value = "/add")
	public String add(Map map) {
		List<Catalog> catalogList = catalogService.getCataloglist();
		List<Brand> brandList = brandService.getAllBrand();
		map.put("catalogList", catalogList);
		map.put("brandList", brandList);
		return "product/product_add";
	}

	@PostMapping(value = "")
	@ResponseBody
	public JSONResult save(ProductVo productVo) {
		JSONResult jsonResultVo = new JSONResult();
		productService.save(productVo);
		return jsonResultVo;

	}

	/**
	 *
	 * @param upload ckedit默认找upload变量名
	 * @param request ckedit上传图片需要用到
	 * @param response ckedit上传图片需要用到
	 */
	@RequestMapping(value = "/ckeditUploadImg")
	public void ckeditUploadImg(MultipartFile upload, HttpServletRequest request, HttpServletResponse response) {
		try {
			//上传文件并返回文件名
			String fileName = UploadUtil.upload(upload, "/" + filePath);
			// 结合ckeditor功能
			// imageContextPath为图片在服务器地址，如/upload/123.jpg,非绝对路径
			String imageContextPath = fileName;
			response.setContentType("text/html;charset=UTF-8");
			String callback = request.getParameter("CKEditorFuncNum");
			PrintWriter out = response.getWriter();
			out.println("<script type=\"text/javascript\">");
			out.println(
					"window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + imageContextPath + "',''" + ")");
			out.println("</script>");
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/uploadImage")
	@ResponseBody
	public String uploadImage(MultipartFile img) {
		String fileName = UploadUtil.upload(img, filePath);
		return "/" + fileName;
	}

	@Reference
	private IProductSkuService productSkuService;

	/**
	 * 生成sku
	 * 
	 * product/+id +skus
	 * @return
	 */
	@GetMapping("/{id}/skus")
	public String generaterSKU(@PathVariable Long id, Model model) {
		Product product = productService.getProductById(id);
		model.addAttribute("product", product);
		List<ProductSku> productSkuList = productSkuService.getSkuProductById(id);
		// 判断商品是否生成过sku,如果生成sku,则跳转到sku管理界面(通过id去查sku信息)
		// 管理可以管理价格,下架,其他是不允许新增的
		if (productSkuList != null && productSkuList.size() > 0) {
			ProductSku productSku = productSkuList.get(0);
			model.addAttribute("skuPropertyList", productSku.getProductSkuPropertyList());
			// 标题属性可以通过sku去获取,属性封装在里面
			model.addAttribute("skuList", productSkuList);
			return "product_sku/sku_list";
		}
		// 否则跳转到sku生成界面
		List<SkuProperty> skuPropertyList = skuPropertyService.getCatalogProperty(product.getCatalog().getId());
		model.addAttribute("skuPropertyList", skuPropertyList);
		return "product_sku/generate_sku";
	}

}
